/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.docdata.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.docdata.entity.EDocument;
import com.thinkgem.jeesite.modules.docdata.service.EDocumentService;

import java.util.List;

/**
 * 公文操作Controller
 *
 * @author 公文操作
 * @version 2018-01-21
 */
@Controller
@RequestMapping(value = "${adminPath}/docdata/eDocument")
public class EDocumentController extends BaseController {

    @Autowired
    private EDocumentService eDocumentService;

    @ModelAttribute
    public EDocument get(@RequestParam(required = false) String id) {
        EDocument entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = eDocumentService.get(id);
        }
        if (entity == null) {
            entity = new EDocument();
        }
        return entity;
    }

    @RequiresPermissions("docdata:eDocument:view")
    @RequestMapping(value = {"list", ""})
    public String list(EDocument eDocument, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<EDocument> page = eDocumentService.findPage(new Page<EDocument>(request, response), eDocument);
        List<EDocument> doclist = page.getList();
        for (EDocument eDoc : doclist) {
        }

        model.addAttribute("page", page);
        return "modules/docdata/eDocumentList";
    }

    @RequiresPermissions("docdata:eDocument:view")
    @RequestMapping(value = "form")
    public String form(EDocument eDocument, Model model) {
        model.addAttribute("eDocument", eDocument);
        return "modules/docdata/eDocumentForm";
    }

    @RequiresPermissions("docdata:eDocument:edit")
    @RequestMapping(value = "save")
    public String save(EDocument eDocument, Model model, RedirectAttributes redirectAttributes) {
        if (!beanValidator(model, eDocument)) {
            return form(eDocument, model);
        }
        eDocumentService.save(eDocument);
        addMessage(redirectAttributes, "保存公文操作成功");
        return "redirect:" + Global.getAdminPath() + "/docdata/eDocument/?repage";
    }

    @RequiresPermissions("docdata:eDocument:edit")
    @RequestMapping(value = "delete")
    public String delete(EDocument eDocument, RedirectAttributes redirectAttributes) {
        eDocumentService.delete(eDocument);
        addMessage(redirectAttributes, "删除公文操作成功");
        return "redirect:" + Global.getAdminPath() + "/docdata/eDocument/?repage";
    }

}